from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import time
from pynput import keyboard,mouse
import threading

startx = 444
starty = 111
xlen = 1340 - startx
ylen = 406-starty

g_co = 0

class my66(QMainWindow):
    def __init__(self):
        super().__init__()        

def ont():
        print(time.time())
        time.sleep(0.3)
        print('sleep over')
        screen  =  QGuiApplication.primaryScreen()        
        img1 = screen.grabWindow(0, startx, starty, xlen, ylen).toImage();            
        keyboard.Controller().press(keyboard.Key.right)
        time.sleep(0.1)
        screen  =  QGuiApplication.primaryScreen()        
        img2 = screen.grabWindow(0, startx, starty, xlen, ylen).toImage();            
        for j in range(ylen):
            for i in range(xlen):
                p = img1.pixel(i,j)
                x1 = QColor(p).getRgb()[:-1]
                p = img2.pixel(i,j)
                x2 = QColor(p).getRgb()[:-1]
                if x1!=x2 and x2[0]==0 and x2[1]==0 and x2[2]==0:
                    print(i,j,x1,x2)
                    mouse.Controller().position = (i+startx,j+starty)
                    return
def tt():
    global g_co
    while True:
        time.sleep(0.1)
        if g_co>0:
            ont()
        g_co = 0
t = threading.Thread(target=tt)
t.setDaemon(True)
t.start()
                

a = QApplication([])  
w = my66()
def on_activate():
    print('Global hotkey activated!')
    global g_co
    g_co += 1

def for_canonical(f):
    return lambda k: f(l.canonical(k))

hotkey = keyboard.HotKey(
    keyboard.HotKey.parse('<alt>+z'),
    on_activate)
l = keyboard.Listener(
        on_press=for_canonical(hotkey.press),
        on_release=for_canonical(hotkey.release))
l.start()
    
w.show() 
a.exec_()
